import 'package:flutter_test/flutter_test.dart';
import 'package:hive_flutter/hive_flutter.dart';
import '../lib/common/services/viewer_service.dart';
import '../lib/common/services/user_service.dart';
import '../lib/common/entity/user.dart';

void main() {
  group('ViewerService Tests', () {
    setUpAll(() async {
      // 初始化Hive用于测试
      await Hive.initFlutter();
      await UserService.initialize();
    });

    tearDownAll(() async {
      await Hive.close();
    });

    test('浏览量为0时不显示头像', () {
      final viewers = ViewerService.generateViewers(
        shareId: 'test_share_1',
        browseCount: 0,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      expect(viewers.length, 0);
    });

    test('浏览量为1时显示1个头像', () {
      final viewers = ViewerService.generateViewers(
        shareId: 'test_share_2',
        browseCount: 1,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      expect(viewers.length, 1);
    });

    test('浏览量为2时显示2个头像', () {
      final viewers = ViewerService.generateViewers(
        shareId: 'test_share_3',
        browseCount: 2,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      expect(viewers.length, 2);
    });

    test('浏览量为3时显示3个头像', () {
      final viewers = ViewerService.generateViewers(
        shareId: 'test_share_4',
        browseCount: 3,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      expect(viewers.length, 3);
    });

    test('浏览量为5时最多显示3个头像', () {
      final viewers = ViewerService.generateViewers(
        shareId: 'test_share_5',
        browseCount: 5,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      expect(viewers.length, 3);
    });

    test('同一个分享ID生成的头像应该一致', () {
      final viewers1 = ViewerService.generateViewers(
        shareId: 'test_share_consistent',
        browseCount: 3,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      final viewers2 = ViewerService.generateViewers(
        shareId: 'test_share_consistent',
        browseCount: 3,
        currentUserId: 'user1',
        hasCurrentUserViewed: false,
      );

      expect(viewers1.length, viewers2.length);
      for (int i = 0; i < viewers1.length; i++) {
        expect(viewers1[i].avatar, viewers2[i].avatar);
        expect(viewers1[i].id, viewers2[i].id);
      }
    });

    test('当前用户浏览过时应该显示在前面', () {
      // 先登录一个用户
      UserService.login('kina', 'kina123');

      final viewers = ViewerService.generateViewers(
        shareId: 'test_share_current_user',
        browseCount: 3,
        currentUserId: '1', // kina的ID
        hasCurrentUserViewed: true,
      );

      expect(viewers.length, 3);
      // 第一个应该是当前用户
      expect(viewers.first.id, '1');
      expect(viewers.first.username, 'kina');
    });

    test('getShareViewers方法应该正常工作', () {
      final viewers = ViewerService.getShareViewers(
        shareId: 'test_share_method',
        browseCount: 2,
        currentUserId: 'user1',
      );

      expect(viewers.length, 2);
    });

    test('getShareDetailViewers方法应该包含当前用户', () {
      // 先登录一个用户
      UserService.login('mark', 'mark123');

      final viewers = ViewerService.getShareDetailViewers(
        shareId: 'test_share_detail',
        browseCount: 2,
        currentUserId: '2', // mark的ID
      );

      expect(viewers.length, 2);
      // 在详情页中，当前用户应该在前面
      expect(viewers.first.id, '2');
      expect(viewers.first.username, 'mark');
    });

    test('未登录用户不应该显示在浏览头像中', () {
      final viewers = ViewerService.getShareViewers(
        shareId: 'test_share_no_user',
        browseCount: 3,
        currentUserId: null, // 未登录
      );

      expect(viewers.length, 3);
      // 所有头像都应该是默认头像，没有真实用户
      for (final viewer in viewers) {
        expect(viewer.id.startsWith('viewer_'), true);
      }
    });

    test('未登录用户在详情页也不显示在浏览头像中', () {
      final viewers = ViewerService.getShareDetailViewers(
        shareId: 'test_share_detail_no_user',
        browseCount: 2,
        currentUserId: null, // 未登录
      );

      expect(viewers.length, 2);
      // 所有头像都应该是默认头像，没有真实用户
      for (final viewer in viewers) {
        expect(viewer.id.startsWith('viewer_'), true);
      }
    });
  });
}
